package com.ivt.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.dao.CoursesDao;
import com.ivt.dao.SpecificDao;
import com.ivt.model.Courses;
import com.ivt.model.Teacher;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.CoursesService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class CoursesServiceImpl implements CoursesService {

	@Resource
	private CoursesDao coursesDao;

	@Resource
	private SpecificDao specificDao;

	@Override
	public Courses update(Courses courses) throws IvtException {
		if (null == courses || null == courses.getId() || 0 >= courses.getId()) {
			throw new IvtException("课程信息不能为空");
		}
		Courses origial = getCourses(courses.getId());
		if (null == origial) {
			throw new IvtException("课程信息参数错误");
		}
		courses.setTeacher(origial.getTeacher());
		courses.setStatus(AuditStatus.PASS);
		return coursesDao.saveAndFlush(courses);
	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryCoursesCount(queryCriteria);
	}

	@Override
	public List<Courses> queryByCriteria(QueryCriteria queryCriteria)
			throws IvtException {

		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryCourses(queryCriteria);
	}

	@Override
	public Courses delete(Long id) throws IvtException {
		if (null == id || 0 >= id) {
			throw new IvtException("参数错误");
		}

		Courses t = getCourses(id);
		t.setStatus(AuditStatus.NOT);
		return coursesDao.saveAndFlush(t);
	}

	@Override
	public int deletes(Long[] ids) throws IvtException {
		if (ArrayUtils.isEmpty(ids)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(ids);
		Iterable<Courses> courses = coursesDao.findAll(cleanIds);
		int i = 0;
		if (null != courses) {
			for (Courses cz : courses) {
				cz.setStatus(AuditStatus.NOT);
				coursesDao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

	@Override
	public Courses getCourses(Long id) throws IvtException {
		if (null == id || 0 >= id) {
			throw new IvtException("参数错误");
		}
		return coursesDao.findOne(id);
	}

	@Override
	public List<Courses> getCourses(Set<Long> ids) throws IvtException {
		if (CollectionUtils.isEmpty(ids)) {
			throw new IvtException("参数错误");
		}

		Iterable<Courses> coll = coursesDao.findAll(ids);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	@Override
	public Courses create(Courses courses) throws IvtException {
		if (null == courses) {
			throw new IvtException("学生信息不能为空");
		}
		if (StringUtils.isBlank(courses.getName())) {
			throw new IvtException("姓名不能为空");
		}

		courses.setId(null);
		courses.setStatus(AuditStatus.PASS);
		return coursesDao.saveAndFlush(courses);
	}

	protected QueryCriteria cleanQueryCriteria(QueryCriteria queryCriteria) {

		if (null == queryCriteria) {
			return null;
		}
		Map<String, Object> criteria = queryCriteria.splitCriteria();

		if (MapUtils.isNotEmpty(criteria)) {
			if (criteria.containsKey("teacher")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("teacher")))) {
				Teacher teacher = new Teacher();
				teacher.setId(Long.valueOf(String.valueOf(criteria
						.get("teacher"))));
				criteria.put("teacher", teacher);
			}

			if (criteria.containsKey("status")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("status")))) {
				AuditStatus status = AuditStatus.findByValue(Integer
						.valueOf(String.valueOf(criteria.get("status"))));
				criteria.put("status", status);
			}
		}

		return queryCriteria;
	}

	@Override
	public List<Courses> getCoursesByTeacher(Teacher teacher)
			throws IvtException {
		return coursesDao.getCoursesByTeacher(AuditStatus.PASS, teacher);
	}
}
